//
//  SSKeychain+LL.m
//  lrlzapp
//
//  Created by dl on 16/2/22.
//  Copyright © 2016年 lrlz. All rights reserved.
//

#import "SSKeychain+LL.h"
#import "OpenUDID.h"

@implementation SSKeychain (LL)

+ (NSString *)UUID {
    NSString *uuid = [self passwordForService:LL_KEYCHAIN_SERVICE_NAME account:LL_KEYCHAIN_UUID];
    if (!uuid) {
        uuid = [OpenUDID value];
        [SSKeychain setUUID:uuid];
    }
    return uuid;
}

+ (NSString *)rawLogin {
    return [[NSUserDefaults standardUserDefaults] objectForKey:LL_NSUSERDEFAULT_RAW_LOGIN];
}

+ (NSString *)password {
    return [self passwordForService:LL_KEYCHAIN_SERVICE_NAME account:LL_KEYCHAIN_PASSWORD];
}

+ (NSString *)accessToken {
    return [self passwordForService:LL_KEYCHAIN_SERVICE_NAME account:LL_KEYCHAIN_ACCESS_TOKEN];
}


+ (BOOL)setUUID:(NSString *)uuid {
    return [self setPassword:uuid forService:LL_KEYCHAIN_SERVICE_NAME account:LL_KEYCHAIN_UUID];
}

+ (BOOL)setRawLogin:(NSString *)rawLogin {
    if (rawLogin == nil) NSLog(@"+setRawLogin: %@", rawLogin);

    [[NSUserDefaults standardUserDefaults] setObject:rawLogin forKey:LL_NSUSERDEFAULT_RAW_LOGIN];
    [[NSUserDefaults standardUserDefaults] synchronize];

    return YES;
}

+ (BOOL)setPassword:(NSString *)password {
    return [self setPassword:password forService:LL_KEYCHAIN_SERVICE_NAME account:LL_KEYCHAIN_PASSWORD];
}

+ (BOOL)setAccessToken:(NSString *)accessToken {
    return [self setPassword:accessToken forService:LL_KEYCHAIN_SERVICE_NAME account:LL_KEYCHAIN_ACCESS_TOKEN];
}


+ (BOOL)deleteUUID {
    return [self deletePasswordForService:LL_KEYCHAIN_SERVICE_NAME account:LL_KEYCHAIN_UUID];
}

+ (BOOL)deleteRawLogin {
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:LL_NSUSERDEFAULT_RAW_LOGIN];
    [[NSUserDefaults standardUserDefaults] synchronize];

    return YES;
}

+ (BOOL)deletePassword {
    return [self deletePasswordForService:LL_KEYCHAIN_SERVICE_NAME account:LL_KEYCHAIN_PASSWORD];
}

+ (BOOL)deleteAccessToken {
    return [self deletePasswordForService:LL_KEYCHAIN_SERVICE_NAME account:LL_KEYCHAIN_ACCESS_TOKEN];
}


+ (BOOL)getLastStateAndLaunch {
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    if (![ud objectForKey:LL_NSUSERDEFAULT_LAST_LAUNCH_VER]
            || [[[NSUserDefaults standardUserDefaults] objectForKey:LL_NSUSERDEFAULT_LAST_LAUNCH_VER] compare:ClientVervion] == NSOrderedAscending) {

        [[NSUserDefaults standardUserDefaults] setObject:ClientVervion forKey:LL_NSUSERDEFAULT_LAST_LAUNCH_VER];
        return NO;
    }
    return YES;
}

@end
